import Core.DatabaseFactory
import Core.UserDao
import Core.Users
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import okhttp3.OkHttpClient
import okhttp3.Request
import okio.IOException
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.select.Elements

class CommandLine {
    init {
        //dbtest()
        //   jsontest()
        //  fetchImages("https://www.qq.com")
        waytest()
    }

    fun waytest() {

        val dynamicObject: MutableMap<String, Any> = HashMap()
        dynamicObject["Name"] = "John"
        dynamicObject["Age"] = 30

        println(dynamicObject)
    }

    fun jsontest() {

//         创建动态对象
        val dynamicObject: MutableMap<String, Any> = HashMap()
        dynamicObject["Name"] = "John"
        dynamicObject["Age"] = 30


        val group: MutableList<Map<String, Any>> = ArrayList()

        // 添加第一个动态对象
        val dynamicObject1: MutableMap<String, Any> = HashMap()
        dynamicObject1["Name"] = "John"
        dynamicObject1["Age"] = 30
        group.add(dynamicObject1)

        // 添加第二个动态对象
        val dynamicObject2: MutableMap<String, Any> = HashMap()
        dynamicObject2["Name"] = "Alice"
        dynamicObject2["Age"] = 25
        group.add(dynamicObject2)

        // 使用Gson库将列表对象转换为JSON字符串
        val gson = Gson()
        val jsonString: String = gson.toJson(group)

        // 使用增强型 for 循环遍历组中的每个动态对象
        for (dynamicObject in group) {
            // 遍历动态对象中的键值对
            dynamicObject.get("Age")
            for ((key, value) in dynamicObject) {
                println("Key: $key, Value: $value")
            }
        }
    }

    fun fetchImages(url: String) {
        try {
            val client = OkHttpClient()
            val request = Request.Builder().url(url).build()
            val response = client.newCall(request).execute()
            val html = response.body?.string()

            Thread {
                Thread.sleep(3000)
                println("123213")
            }.start()

            if (html != null) {
                val doc: Document = Jsoup.parse(html)
                val imageElements: Elements = doc.select("img")

                val imageUrls = imageElements.map { it.attr("src") }
//                    _images.postValue(imageUrls)
                var a = 1
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    private fun dbtest() {
        DatabaseFactory.init()

        transaction {
            SchemaUtils.create(Users)
        }
        // Example usage
        transaction {
            val user = UserDao.new {
                name = "John Doe"
                age = 30
            }
            UserDao.new {
                name = "John Doe"
                age = 30
            }
            UserDao.new {
                name = "John Doe"
                age = 30
            }
            UserDao.new {
                name = "John Doe"
                age = 30
            }
            println("Inserted user: ${user.name}, age: ${user.age}")

            val users = UserDao.all().toList()
            users.forEach {
                println("User: ${it.name}, age: ${it.age}")
            }
        }
    }
}